<template>
    <el-dialog v-model="visible" :title="form.id ? '修改' : '添加'" @closed="onReset" :close-on-click-modal="false">
        <el-form :model="form" ref="formRef" label-width="100px">
            <el-form-item label="类型">
                <el-select v-model="form.type" placeholder="请选择" clearable>
                    <el-option :label="item.label" :value="item.value" v-for="item in extinguisherOption"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="型号">
                <el-input v-model="form.model" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="厂家">
                <el-input v-model="form.factory" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="数量">
                <el-input v-model="form.quantity" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="出厂日期">
                <el-date-picker v-model="form.date_of_manufacture" type="date" placeholder="请选择"
                    value-format="YYYY-MM-DD" />
            </el-form-item>
            <el-form-item label="上次维修日期">
                <el-date-picker v-model="form.last_maintenance_date" type="date" placeholder="请选择"
                    value-format="YYYY-MM-DD" />
            </el-form-item>
            <el-form-item label="维修日期">
                <el-date-picker v-model="form.maintenance_period" type="date" placeholder="请选择"
                    value-format="YYYY-MM-DD" />
                <!-- <el-select v-model="form.maintenance_period" placeholder="请选择" clearable>
          <el-option :label="item.label" :value="item.value" v-for="item in extinguisherWarnOption"></el-option>
        </el-select> -->
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="visible = false">取消</el-button>
                <el-button type="primary" @click="onConfirm">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { daoqichaxunAddOrUpdate } from '@/api/miehuoqi'
import { extinguisherOption, extinguisherWarnOption } from '@/util/explain'
const emit = defineEmits(['refresh'])
const visible = ref(false)
const formRef = ref()
const form = reactive({
    id: 0,
    type: '',
    model: '',
    factory: '',
    date_of_manufacture: '',
    maintenance_period: '',
    quantity: '',
    last_maintenance_date: ''
})
const resetForm = JSON.parse(JSON.stringify(form))
const open = (data?) => {
    visible.value = true
    if (data) Object.assign(form, { ...data })
}
const onConfirm = async () => {
    await formRef.value.validate()

        daoqichaxunAddOrUpdate(form).then(({ msg }: any) => {
            ElMessage.success(msg)
            visible.value = false
            emit('refresh')
        })
}
const onReset = () => {
    Object.keys(resetForm).forEach((item: string) => {
        form[item] = resetForm[item]
    })
}
defineExpose({ open })
</script>

<style lang="scss" scoped></style>
